﻿package com.best.oasis.settlement.biz.facade.security
{
	import com.best.oasis.flexbase.util.BeanUtils;
	import com.best.oasis.settlement.biz.facade.base.OldBaseFacade;
	import com.best.oasis.settlement.biz.facade.base.SuperResponder;
	import com.best.oasis.settlement.common.error.ErrorCode;
	import com.best.oasis.settlement.common.model.ModelLocator;
	import com.best.oasis.settlement.common.vo.admin.authentication.AuthenticationVO;
	import com.best.oasis.settlement.common.vo.security.UserVO;
	import com.best.oasis.settlement.common.vo.security.UsernamePasswordAuthenticationToken;
	
	import mx.controls.Alert;
	import mx.rpc.AsyncToken;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	/**
	 * 用户登录、注销等操作 
	 * @author bl00064
	 * 
	 */	
	public class AuthenticationFacade extends OldBaseFacade
	{
		
		
		
		/**
		 * 登录 
		 * @param vo
		 * @param callback
		 * 
		 */
		public function login(vo:AuthenticationVO, callback:Function = null,callback2:Function = null):void{
			var responder:SuperResponder = new SuperResponder(
				function(event:ResultEvent):void{
					if(event.result != null){
						var token:UsernamePasswordAuthenticationToken =
								UsernamePasswordAuthenticationToken(event.result);
						if(token.authenticated){
							if(callback!=null){
								callback();
							}	
							if(callback2!=null){
								callback2();
							}
						}else{
							if(token.details != null 
								&& token.details.toString() == ErrorCode.USER_NOT_EXIST){
								Alert.show("用户名或密码错误，请重新输入。");
							}
							if(callback2!=null){
								callback2();
							}
							
						}
					}else{
						Alert.show("登录失败！");
					}
				}, 
				function(event:FaultEvent):void{
					if(event.fault.faultString == ErrorCode.BAD_CREDENTIALS){
						Alert.show("密码错误！请重新输入。");
					}else if(event.fault.faultString == ErrorCode.USER_NOT_EXIST){
						Alert.show("用户名或密码错误。");
					}else if(event.fault.faultString == ErrorCode.SEND_FAILED){
						Alert.show("连接服务器出错。");
					}else if(event.fault.faultString == ErrorCode.USER_NOT_FOUND){
						Alert.show("用户名或密码错误。");
					}else{
						Alert.show(event.fault.faultString);
					}
					if(callback2!=null){
						callback2();
					}	
				});
			delegate.login(vo.username,vo.password).addResponder(responder);
			
		}
		
		/**
		 * 获取当前登录用户信息 
		 * @param result
		 * @param callback
		 * 
		 */
		public function getLoginUser(result:UserVO, callback:Function = null):void{
			var responder:SuperResponder = new SuperResponder(
				function(event:ResultEvent):void{
					if(event.result != null){
						var vo:UserVO = UserVO(event.result);
						BeanUtils.copyProperties(result, vo);
						if(callback != null){
							callback();
						}
					}else{
						Alert.show("获取当前登录用户信息失败！");
					}
				}, 
				function(event:FaultEvent):void{
					Alert.show("获取当前登录用户信息失败:" + event.fault.faultString);
				});
			
			delegate.getLoginUser().addResponder(responder);
		}
		
		/**
		 * 注销 
		 * @param callback
		 * 
		 */
		public function logout(callback:Function = null):void{
			var responder:SuperResponder = new SuperResponder(
				function(event:ResultEvent):void{
					if(event.result != null){
						ModelLocator.getInstance().mainContentViewStackSelectedIndex = 0
						ModelLocator.getInstance().authMsg = '';
						if(callback!=null){
							callback();
						}	
					}else{
						Alert.show("注销失败！");
					}
				}, 
				function(event:FaultEvent):void{
					Alert.show("注销失败！" + event.toString());
				});
		
			delegate.doLogout().addResponder(responder);
		}
	}
}